package com.xatu.yl.storeshopping.activity;

//这个是主界面
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.namespace.R;
import com.xatu.yl.storeshopping.activity.Fragment.AwaitDeliveryFragment;
import com.xatu.yl.storeshopping.activity.Fragment.UserHomeFragment;
import com.xatu.yl.storeshopping.activity.Fragment.UserMineFragment;
import com.xatu.yl.storeshopping.util.ChangeStateUtil;


public class UserMainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView home,awaitDelivery,mine;
    private ImageButton homeImage,awaitDeliveryImage,mineImage;
    private ListView pro_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_main);
        // 隐藏 ActionBar
        getSupportActionBar().hide();

        //底部导航栏，点击切换功能
        //按钮
        homeImage=findViewById(R.id.homeImage);
        awaitDeliveryImage=findViewById(R.id.awaitDeliveryImage);
        mineImage=findViewById(R.id.mineImage);

        //文字
        home=findViewById(R.id.home_text);
        awaitDelivery=findViewById(R.id.await_delivery_text);
        mine=findViewById(R.id.mine_text);
        home.setOnClickListener(this);
        awaitDelivery.setOnClickListener(this);
        mine.setOnClickListener(this);

        //按钮
        homeImage.setOnClickListener(this);
        awaitDeliveryImage.setOnClickListener(this);
        mineImage.setOnClickListener(this);

        //第一次进入这个页面,home显示被选中
        ChangeStateUtil.changeOk(home,17,homeImage);
        //每次打开APP，先展示首页内容
        replaceFragment(new UserHomeFragment());
    }

    //底部导航栏
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.home_text || v.getId()==R.id.homeImage){
            //将被点击的组建设置为不同的状态
            ChangeStateUtil.changeOk(home,18,homeImage);
            ChangeStateUtil.changeNo(awaitDelivery,15,awaitDeliveryImage);
            ChangeStateUtil.changeNo(mine,15,mineImage);
            replaceFragment(new UserHomeFragment());
        }
        if(v.getId()==R.id.await_delivery_text || v.getId()==R.id.awaitDeliveryImage){
            ChangeStateUtil.changeOk(awaitDelivery,18,awaitDeliveryImage);
            ChangeStateUtil.changeNo(home,15,homeImage);
            ChangeStateUtil.changeNo(mine,15,mineImage);
            replaceFragment(new AwaitDeliveryFragment());
        }
        if(v.getId()==R.id.mine_text || v.getId()==R.id.mineImage){
            ChangeStateUtil.changeOk(mine,18,mineImage);
            ChangeStateUtil.changeNo(home,15,homeImage);
            ChangeStateUtil.changeNo(awaitDelivery,15,awaitDeliveryImage);
            //切换
            replaceFragment(new UserMineFragment());
        }
    }



    //点击切换fragment，动态
    private void replaceFragment(Fragment endFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //其实多个fragment之间的切换：本质上就是从一个根fragment切换到目标fragment的过程
        transaction.replace(R.id.homeFragment, endFragment);
        //提交事件
        transaction.commit();
    }
}